﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.ComponentModel;

namespace IronCow.Posh.Commands
{
    [Cmdlet(VerbsCommon.Get, "RtmTaskList", DefaultParameterSetName = "Name")]
    [System.ComponentModel.Description("Gets RTM task lists.")]
    public class GetRtmTaskListCommand : RtmCmdlet
    {
        [Parameter(
            Mandatory = false,
            Position = 1,
            ParameterSetName = "Name",
            ValueFromPipeline = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The name of the task list to get. Wildcards are supported.")]
        [ValidateNotNullOrEmpty]
        [DefaultValue(null)]
        public string Name { get; set; }

        [Parameter(
            Mandatory = true,
            ParameterSetName = "Id",
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The ID of the task list to get.")]
        [Alias("ListId", "TaskListId")]
        [ValidateRange(1, int.MaxValue)]
        public string Id { get; set; }

        [Parameter(
            Mandatory = true,
            ParameterSetName = "Type",
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The type of task lists to get.")]
        public TaskListFlags Type { get; set; }

        public GetRtmTaskListCommand()
        {
            Id = null;
            Name = null;
            Type = TaskListFlags.None;
        }

        protected override void ProcessRecord()
        {
            switch (ParameterSetName)
            {
                case "Name":
                    if (string.IsNullOrEmpty(Name))
                    {
                        WriteObject(Rtm.TaskLists, true);
                    }
                    else
                    {
                        WildcardPattern pattern = new WildcardPattern(Name, WildcardOptions.IgnoreCase);
                        foreach (TaskList taskList in Rtm.TaskLists)
                        {
                            if (pattern.IsMatch(taskList.Name))
                                WriteObject(taskList);
                        }
                    }
                    break;
                case "Id":
                    WriteObject(Rtm.TaskLists.GetById(Id, true));
                    break;
                case "Type":
                    foreach (TaskList taskList in Rtm.TaskLists)
                    {
                        if (taskList.GetFlag(Type))
                            WriteObject(taskList);
                    }
                    break;
            }
        }
    }
}
